package com.shoulder.boot.ops.utils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shoulder.boot.ops.dto.MachineQueryDTO;
import com.shoulder.boot.ops.entity.Machine;
import com.shoulder.boot.ops.vo.MachineVo;
import com.shoulder.mybatis.utils.PageUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.ObjectUtils;

public class MachineUtils implements PageUtils<Machine, MachineVo> {

    public static MachineUtils buildUtils() {
        return new MachineUtils();
    }

    @Override
    public MachineVo toVo(Machine entity) {
        MachineVo vo = new MachineVo();
        BeanUtils.copyProperties(entity, vo);
        return vo;
    }

    public static LambdaQueryWrapper<Machine> buildQueryWrapper(MachineQueryDTO queryDTO) {
        LambdaQueryWrapper<Machine> queryWrapper = new LambdaQueryWrapper<Machine>().orderByDesc(Machine::getCreateTime);
        queryWrapper.like(!ObjectUtils.isEmpty(queryDTO.getName()), Machine::getName, queryDTO.getName());
        queryWrapper.like(!ObjectUtils.isEmpty(queryDTO.getHost()), Machine::getHost, queryDTO.getHost());
        queryWrapper.eq(!ObjectUtils.isEmpty(queryDTO.getPort()), Machine::getPort, queryDTO.getPort());
        queryWrapper.like(!ObjectUtils.isEmpty(queryDTO.getUsername()), Machine::getUsername, queryDTO.getUsername());
        queryWrapper.like(!ObjectUtils.isEmpty(queryDTO.getPassword()), Machine::getPassword, queryDTO.getPassword());
        queryWrapper.like(!ObjectUtils.isEmpty(queryDTO.getTag()), Machine::getTag, queryDTO.getTag());
        return queryWrapper;
    }
}
